package org.yscz.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.yscz.common.HttpStatus;
import org.yscz.common.Result;
import org.yscz.entity.Scale;
import org.yscz.entity.Score;
import org.yscz.entity.vo.QuestionVo;
import org.yscz.entity.vo.ScaleVo;
import org.yscz.mapper.ScoreMapper;
import org.yscz.service.RecordService;
import org.yscz.service.ScaleService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 量表接口
 *
 * @author muqs
 * @create 2021-08-19 18:45
 */
@RestController
@Slf4j
@Api("量表接口")
public class ScaleController {

    @Autowired
    private ScaleService scaleService;

    @Autowired
    private RecordService recordService;

    @Resource
    private ScoreMapper scoreMapper;

    /**
     * 新增量表接口
     *
     * @param scaleVo 前台传递的量表数据
     * @return Result对象
     */
    @PostMapping("/scale/add")
    @ApiOperation(value = "新增量表接口", httpMethod = "POST")
    public Result add(@RequestBody ScaleVo scaleVo) {
        log.info("scaleVo:[{}]", scaleVo);
        return Result.success(HttpStatus.SUCCESS, "新增量表成功", scaleService.add(scaleVo));
    }

    /**
     * 修改量表接口
     *
     * @param scaleVo 前台传递的量表数据
     * @return Result对象
     */
    @PostMapping("/scale/modify")
    @ApiOperation(value = "修改量表接口", httpMethod = "POST")
    public Result modify(@RequestBody ScaleVo scaleVo) {
        log.info("scaleVo:[{}]", scaleVo);

        scaleService.modify(scaleVo);
        return Result.success(HttpStatus.SUCCESS, "修改量表成功", scaleVo);
    }

    /**
     * 删除量表接口
     *
     * @param scaleId 前台传递的量表数据
     * @return Result对象
     */
    @DeleteMapping("/scale/del/{scaleId}")
    @ApiOperation(value = "删除量表接口", httpMethod = "DELETE")
    public Result del(@PathVariable Long scaleId) {
        log.info("scaleId:[{}]", scaleId);
        scaleService.del(scaleId);
        return Result.success(HttpStatus.SUCCESS, "删除量表成功", "");
    }

    /**
     * 查询量表列表接口
     *
     * @param scaleName 量表名
     * @param pageNum   当前页
     * @param pageSize  每页数据条数
     * @return Result对象
     */
    @GetMapping("/scale/listPage")
    @ApiOperation(value = "查询量表接口", httpMethod = "GET")
    public Result listPage(@RequestParam(value = "scaleName", required = false) String scaleName,
                           @RequestParam(value = "scaleTypeIdFk", required = false) Long scaleTypeIdFk,
                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        JSONObject reusltObj = new JSONObject();
        Map condition = new HashMap();
        IPage<Scale> page = new Page(pageNum, pageSize);
        if (!StringUtils.isEmpty(scaleName)) {
            condition.put("scaleName", "%" + scaleName.replaceAll(" ", "") + "%");
        }

        if (scaleTypeIdFk != null) {
            condition.put("scaleTypeIdFk", scaleTypeIdFk);
        }

        // 添加排序规则，
        if (Integer.valueOf(5).equals(pageSize)){
            condition.put("type",Integer.valueOf(1));
        } else if (Integer.valueOf(6).equals(pageSize)){
            condition.put("type",Integer.valueOf(2));
        }

        IPage<ScaleVo> scalePage = scaleService.listPageByCondition(page, condition);

        reusltObj.put("list", scalePage == null ? new ArrayList<>() : scalePage.getRecords());
        reusltObj.put("total", scalePage == null ? 0 : scalePage.getTotal());

        return Result.success(HttpStatus.SUCCESS, "查询量表列表成功", reusltObj);
    }

    /**
     * 新增试题接口
     *
     * @param questionVoList 试题信息集合
     * @return Result对象
     */
    @PostMapping("/question/add")
    @ApiOperation(value = "新增试题接口", httpMethod = "POST")
    public Result questionAdd(@RequestBody List<QuestionVo> questionVoList) {
        if (!CollectionUtils.isEmpty(questionVoList)) {
            scaleService.questionAdd(questionVoList);
        }

        return Result.success(HttpStatus.SUCCESS, "新增试题成功", "");
    }

    /**
     * 修改试题接口
     *
     * @param questionVoList 试题信息集合
     * @return Result对象
     */
    @PostMapping("/question/modify")
    @ApiOperation(value = "修改试题接口", httpMethod = "POST")
    public Result questionModify(@RequestBody List<QuestionVo> questionVoList) {
        if (!CollectionUtils.isEmpty(questionVoList)) {
            scaleService.questionModify(questionVoList);
        }

        return Result.success(HttpStatus.SUCCESS, "修改试题成功", "");
    }

    /**
     * 根据量表ID获取试题信息
     *
     * @param scaleId 量表ID
     * @return Result对象
     */
    @GetMapping("/question/queryByScaleId/{scaleId}")
    @ApiOperation(value = "根据量表ID获取试题信息接口", httpMethod = "GET")
    public Result queryByScaleId(@PathVariable Long scaleId) {
        if (scaleId == null) {
            return Result.success(HttpStatus.BAD_REQUEST, "参数错误", "");
        }

        return Result.success(HttpStatus.SUCCESS, "获取试题信息成功", scaleService.queryByScaleId(scaleId));
    }

    /**
     * 获取量表详情
     *
     * @param scaleId 量表ID
     * @return Result对象
     */
    @GetMapping("/scale/detail/{scaleId}")
    @ApiOperation(value = "获取量表详情接口", httpMethod = "GET")
    public Result queryScaleDetail(@PathVariable Long scaleId) {
        if (scaleId == null) {
            return Result.success(HttpStatus.BAD_REQUEST, "参数错误", "");
        }

        return Result.success(HttpStatus.SUCCESS, "获取量表详情成功", scaleService.queryScaleDetail(scaleId));
    }

    /**
     * 获取被测人未完成量表
     *
     * @param testeeId 被测人姓名
     * @return Result对象
     */
    @GetMapping("/scale/recordInfo")
    @ApiOperation(value = "选择量表接口", httpMethod = "GET")
    public Result recordInfo(@RequestParam(value = "testeeId") Long testeeId) {
        if (testeeId == null) {
            return Result.success(HttpStatus.BAD_REQUEST, "参数错误", "");
        }

        return Result.success(HttpStatus.SUCCESS, "选择量表操作成功", recordService.queryRecordDetail(testeeId, 0));
    }


    /**
     * 获取被测人未完成量表
     *
     * @return Result对象
     */
    @GetMapping("/scale/testCore")
    @ApiOperation(value = "选择量表接口", httpMethod = "GET")
    public Result recordInfo(@RequestParam(value = "scaleId") Long scaleId,
                             @RequestParam(value = "score") Integer score) {
        // 根据测评分数获取测评结果;
        Map param = new HashMap();
        param.put("scaleId", scaleId);
        param.put("score", score);
        Score scoreResult = scoreMapper.queryScoreContent(param);

        return Result.success(HttpStatus.SUCCESS, "选择量表操作成功", scoreResult);
    }
}
